package com.ophelisis.common.util;

import java.io.IOException;

import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ophelisis.common.consts.ConstPath;

@SuppressWarnings("serial")
public class PagingTag extends TagSupport {
	protected static Log logger = LogFactory.getLog(PagingTag.class);

	private int totalCnt; // 전체 글 수
	private String action; // <form action=...

	private int startRowNo; // 해당 페이지의 시작 글순번

	private int displyPageCnt = 10; // 페이지 목록에 보여지는 페이지 갯수
	private int displyRowCnt; // 페이지 당 글 수
	private int lastStartRowNo; // 마지막 페이지 startRowNo
	
	@SuppressWarnings("unused")
	private String imgSrc = (ConstPath.prefixName == "" ? "" : ("/" + ConstPath.prefixName));
	
	public int doStartTag() throws JspTagException {
		// 전체페이지수 계산
		int totalPageCnt = totalCnt / displyRowCnt;
		if ((totalCnt % displyRowCnt) > 0)
			totalPageCnt++;

		// 현재 페이지번호 계산
		int currentPageNo = startRowNo / displyRowCnt;

		if (currentPageNo == 0)
			currentPageNo = 1;
		else
			currentPageNo++;

		// 이전 페이지 startRowNo
		int preStartRowNo = 0;
		preStartRowNo = startRowNo - displyRowCnt;

		// 다음 페이지startRowNo
		int nextStartRowNo = 0;
		nextStartRowNo = startRowNo + displyRowCnt;

		// 현재 페이지 그룹 시직번호
		int pageGroupStart = 0;
		if (currentPageNo % displyPageCnt == 0) { // 시작 페이지 번호 계산
			pageGroupStart = ((currentPageNo / displyPageCnt) * displyPageCnt) - (displyPageCnt - 1);
		} else {
			pageGroupStart = ((currentPageNo / displyPageCnt) * displyPageCnt) + 1;
		}

		// 현재 페이지 그룹 끝번호
		int pageGroupEnd = 0;
		if (pageGroupStart == ((totalPageCnt / displyPageCnt) * displyPageCnt + 1)) { // 끝 페이지 번호 계산
			pageGroupEnd = pageGroupStart + (totalPageCnt % displyPageCnt - 1);
		} else {
			pageGroupEnd = pageGroupStart + (displyPageCnt - 1);
		}

		// 마지막 페이지 startRowNo
		if (totalCnt % displyRowCnt == 0) {
			lastStartRowNo = ((totalCnt / displyRowCnt) - 1) * displyRowCnt;;
		} else {
			lastStartRowNo = (totalCnt / displyRowCnt) * displyRowCnt;;
		}
		try {
			StringBuffer htmlSb = new StringBuffer();

			htmlSb.append("<form method=\"post\" action=\"" + action + "\" id=\"pageForm\" name=\"pageForm\">\n");
			htmlSb.append("  <input type=\"hidden\" id=\"startRowNo\" name=\"startRowNo\" value=\"\">\n");
			htmlSb.append("<div id='pagenumber'>");

			if (preStartRowNo >= 0) {
				htmlSb.append("<a href=\"javascript:movePage(0);\"><img src=\"images/btn/prew2.gif\" style=\"margin:0px 0px 0px 0px;\" border=\"0\" title=\"첫목록\" /></a>");
				htmlSb.append("<a href=\"javascript:movePage(" + preStartRowNo + ");\"><img src=\"images/btn/prew1.gif\" style=\"margin:0px 5px 0px 0px;\" border=\"0\" title=\"이전목록\" /></a>");
			} else {
				htmlSb.append("<a><img src=\"images/btn/prew2.gif\" style=\"margin:0px 0px 0px 0px;\" title=\"첫목록\" /></a>");
				htmlSb.append("<a><img src=\"images/btn/prew1.gif\" style=\"margin:0px 5px 0px 0px;\" title=\"이전목록\" /></a>");
			}

			for (int i = pageGroupStart; i <= pageGroupEnd; i++) {
				if (i == currentPageNo) {
					htmlSb.append("");
					if (i < displyPageCnt && i < pageGroupEnd) {
						htmlSb.append("<a class='on'>" + i + "</a>");
					} else {
						htmlSb.append("<a class='on'>" + i + "</a>");
					}
					htmlSb.append("");
				} else {
					htmlSb.append("");
					if (i < displyPageCnt && i < pageGroupEnd) {
						htmlSb.append("<a href=\"javascript:movePage(" + (i * displyRowCnt - displyRowCnt) + ");\">" + i + "</a>");
					} else {
						htmlSb.append("<a href=\"javascript:movePage(" + (i * displyRowCnt - displyRowCnt) + ");\">" + i + "</a>");
					}
					htmlSb.append("");
				}
			}

			if (nextStartRowNo < totalCnt) {
				htmlSb.append("<a href=\"javascript:movePage(" + nextStartRowNo + ");\"><img src=\"images/btn/next1.gif\" style=\"margin:0px 0px 0px 5px;\" border=\"0\" title=\"다음목록\" /></a>");
				htmlSb.append("<a href=\"javascript:movePage(" + lastStartRowNo + ");\"><img src=\"images/btn/next2.gif\" style=\"margin:0px 0px 0px 0px;\" border=\"0\" title=\"끝목록\" /></a>");
			} else {
				htmlSb.append("<a><img src=\"images/btn/next1.gif\" style=\"margin:0px 0px 0px 5px;\" title=\"다음목록\" /></a>");
				htmlSb.append("<a><img src=\"images/btn/next2.gif\" style=\"margin:0px 0px 0px 0px;\" title=\"끝목록\" /></a>");
			}
			pageContext.getOut().print(htmlSb);
		} catch (IOException ioe) {
		}

		return (EVAL_BODY_INCLUDE);
	}

	public int doEndTag() {
		try {
			StringBuffer htmlSb = new StringBuffer();
			htmlSb.append("</div>\n");
			htmlSb.append("</form>\n");
			pageContext.getOut().print(htmlSb);
		} catch (IOException ioe) {
		}
		return (EVAL_PAGE);
	}

	public void setStartRowNo(String startRowNo) {
		this.startRowNo = Integer.parseInt(startRowNo);
	}

	public void setStartRowNo(int startRowNo) {
		this.startRowNo = startRowNo;
	}

	public void setTotalCnt(String totalCnt) {
		this.totalCnt = Integer.parseInt(totalCnt);
	}

	public void setTotalCnt(int totalCnt) {
		this.totalCnt = totalCnt;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public void setDisplyRowCnt(String displyRowCnt) {
		this.displyRowCnt = Integer.parseInt(displyRowCnt);
	}

	public void setDisplyRowCnt(int displyRowCnt) {
		this.displyRowCnt = displyRowCnt;
	}
}
